package com.restcloud.run.PI;

import com.restcloud.pojo.PersonalInformation.*;
import com.restcloud.pojo.Public.Code;
import com.restcloud.pojo.Public.Id;
import com.restcloud.pojo.Public.Name;
import com.restcloud.pojo.Public.Value;
import com.restcloud.utils.TimeUtil;
import com.restcloud.utils.XmlUtil;

import java.util.ArrayList;
import java.util.List;

public class Query {

    /**
     * 个人信息查询
     * @param args
     */
    public static void main(String[] args) {
        PRPA_IN201306UV02 prpa = new PRPA_IN201306UV02();
        prpa.setXmlns("urn:hl7-org:v3");
        prpa.setITSVersion("XML_ 1.0");
        prpa.setXmlnsXsi("http://www.w3.org/2001/XMLSchema-instance");
        prpa.setSchemaLocation("urn:hl7-org:v3 ../multicacheschemas/PRPA_IN201306UV02.xsd");
        /**
         * 消息本身的 ID，需保证 院内唯一性
         */
        Id id = new Id();
        id.setExtension("8D73520B-D489-4B70-8F4B-7B5C2D7961B5");
        id.setRoot("2.16.156.10011.0");
        prpa.setId(id);
        /**
         * 创建时间
         */
        Value creationTime = new Value(TimeUtil.dateTime());
        prpa.setCreationTime(creationTime);

        Id interactionId = new Id();
        interactionId.setRoot("2.16.840.1.113883.1.6");
        interactionId.setExtension("PRPA_IN201306UV02");
        prpa.setInteractionId(interactionId);

        prpa.setProcessingCode(new Code("P"));

        prpa.setProcessingModeCode(new Code());

        prpa.setAcceptAckCode(new Code("AL"));

        Receiver receiver = new Receiver();
        receiver.setTypeCode("RCV");
        receiver.setTelecom(new Code());
        Device device = new Device();
        device.setClassCode("DEV");
        device.setDeterminerCode("INSTANCE");
        List<Id> idItem1 = new ArrayList<>();
        idItem1.add(new Id("2.16.156.10011.0.1.1",""));
        device.setId(idItem1);
        receiver.setDevice(device);
        prpa.setReceiver(receiver);

        Receiver sender = new Receiver();
        sender.setTypeCode("SND");
        sender.setTelecom(new Code());
        Device device1 = new Device();
        device1.setClassCode("DEV");
        device1.setDeterminerCode("INSTANCE");
        List<Id> idItem2 = new ArrayList<>();
        idItem2.add(new Id("2.16.156.10011.0.1.1",""));
        device1.setId(idItem2);
        sender.setDevice(device1);
        prpa.setSender(sender);

        Receiver acknowledgement = new Receiver();
        acknowledgement.setTypeCode("AA");
        Value targetMessage = new Value();
        Id tarId =new Id();
        tarId.setRoot("2.16.156.10011.0");
        tarId.setExtension("22a0f9e0-4454-11dc-a6be-3603d6866807");
        targetMessage.setId(tarId);
        acknowledgement.setTargetMessage(targetMessage);
        prpa.setAcknowledgement(acknowledgement);

        Device controlActProcess = new Device();
        controlActProcess.setClassCode("CACT");
        controlActProcess.setMoodCode("EVN");
        Code code = new Code("PRPA_TE201306UV02","2.16.840.1.113883.1.6");
        controlActProcess.setCode(code);
        Receiver subject = new Receiver();
        subject.setTypeCode("SUBJ");
        Device registrationEvent = new Device();
        registrationEvent.setClassCode("REG");
        registrationEvent.setMoodCode("EVN");
        registrationEvent.setStatusCode(new Code("active"));

        Receiver subject1 = new Receiver("SBJ");
        Device patient = new Device("PAT");
        List<Id> idItem3 = new ArrayList<>();
        idItem3.add(new Id("2.16.156.10011.0.2.1","患者ID"));
        patient.setId(idItem3);
        patient.setStatusCode(new Code("active"));
        patient.setEffectiveTime(new Code(new Value("20100101"),new Value("20501231")));
        /**
         * 个人信息
         */
        PatientPerson patientPerson = new PatientPerson();
        /**
         * 身份证号
         */
        List<Id> listCard = new ArrayList<>();
        listCard.add(new Id("2.16.156.10011.1.3","120109197706015516"));
        patientPerson.setId(listCard);
        /**
         * 姓名
         */
        Name name1 = new Name("LIST_EN");
        List<Value> list = new ArrayList<>();
        list.add(new Value("刘永好"));
        name1.setItem(list);
        patientPerson.setName(name1);
        /**
         * 联系电话
         */
        Name telecom = new Name("DSET_TEL");
        telecom.setValue(new Value("028-2222444"));
        patientPerson.setTelecom(telecom);
        /**
         * 性别
         */
        Code administrativeGenderCode = new Code("1","2.16.156.10011.2.3.3.4");
        administrativeGenderCode.setDisplayName(new Value("男性"));
        patientPerson.setAdministrativeGenderCode(administrativeGenderCode);
        /**
         * 出生时间
         */
        patientPerson.setBirthTime(new Value("19570323"));
        /**
         * 联系地址
         */
        Name addr = new Name("LIST_AD");
        List<Value> addrs = new ArrayList<>();
        addrs.add(new Value("SAL","四川省成都市双流县红沙村"));
        addrs.add(new Value("STA","广东省"));
        addrs.add(new Value("CTY","广州市"));
        addrs.add(new Value("CNT","越秀区"));
        addrs.add(new Value("STB","童心街"));
        addrs.add(new Value("STR","下塘西路"));
        addrs.add(new Value("BNR","39号"));
        addrs.add(new Value("ZIP","510000"));
        addr.setItem(addrs);
        patientPerson.setAddr(addr);
        /**
         * 婚姻状况
         */
        Code maritalStatusCode = new Code("10","2.16.156.10011.2.3.3.5");
        maritalStatusCode.setDisplayName(new Value("未婚"));
        patientPerson.setMaritalStatusCode(maritalStatusCode);
        /**
         * 民族
         */
        Value ethnicGroupCode = new Value();
        Code ethnic_item = new Code("01","2.16.156.10011.2.3.3.3");
        ethnic_item.setDisplayName(new Value("汉族"));
        ethnicGroupCode.setItem(ethnic_item);
        patientPerson.setEthnicGroupCode(ethnicGroupCode);
        /**
         * 职业类别代码
         * 工作单位相关信息
         */
        Device asEmployee = new Device("EMP");
        Code occupationCode = new Code("13","2.16.156.10011.2.3.3.7");
        occupationCode.setDisplayName(new Value("专业技术人员"));
        asEmployee.setOccupationCode(occupationCode);
        Device employerOrganization = new Device("ORG","INSTANCE");
        Name name2 = new Name("LIST_EN");
        List<Value> school = new ArrayList<>();
        school.add(new Value("XXX学校"));
        name2.setItem(school);
        employerOrganization.setName(name2);
        Device empTelecom = new Device("CON");
        Name DSET_TEL = new Name("DSET_TEL");
        DSET_TEL.setValue(new Value("028-9999999"));
        empTelecom.setTelecom(DSET_TEL);
        employerOrganization.setContactParty(empTelecom);
        asEmployee.setEmployerOrganization(employerOrganization);
        patientPerson.setAsEmployee(asEmployee);
        /**
         * 职业类别代码
         * 健康卡号相关信息
         * 城乡居民健康档案相关信息
         */
        List<Device> asOtherIDs = new ArrayList<>();
        // 健康卡号
        Device as1 = new Device("PAT");
        List<Id> idItem4 = new ArrayList<>();
        idItem4.add(new Id("2.16.156.10011.1.19","38273N237"));
        as1.setId(idItem4);
        Device scop1 = new Device("ORG","INSTANCE");
        List<Id> idItem5 = new ArrayList<>();
        idItem5.add(new Id("2.16.156.10011.1.5","XXXXX"));
        scop1.setId(idItem5);
        as1.setScopingOrganization(scop1);
        asOtherIDs.add(as1);
        // 城乡居民健康档案编号
        Device as2 = new Device("PAT");
        List<Id> idItem6 = new ArrayList<>();
        idItem6.add(new Id("2.16.156.10011.1.19","38273N237"));
        as2.setId(idItem6);
        Device scop2 = new Device("ORG","INSTANCE");
        List<Id> idItem7 = new ArrayList<>();
        idItem7.add(new Id("2.16.156.10011.1.5","XXXXX"));
        scop2.setId(idItem7);
        as2.setScopingOrganization(scop2);
        asOtherIDs.add(as2);
        patientPerson.setAsOtherIDs(asOtherIDs);
        /**
         * 联系人
         */
        Device personalRelationship = new Device();
        personalRelationship.setCode(new Code());
        Name personTel = new Name("DSET_TEL");
        personTel.setValue(new Value("028-8888888"));
        personalRelationship.setTelecom(personTel);
        Device relationshipHolder1 = new Device("PSN","INSTANCE");
        Name relationName = new Name("LIST_EN");
        List<Value> relationList = new ArrayList<>();
        relationList.add(new Value("刘永好"));
        relationName.setItem(relationList);
        relationshipHolder1.setName(relationName);
        personalRelationship.setRelationshipHolder1(relationshipHolder1);
        patientPerson.setPersonalRelationship(personalRelationship);

        patient.setPatientPerson(patientPerson);
        /**
         * 医疗保险信息
         */
        Device providerOrganization = new Device("ORG","INSTANCE");
        List<Id> idItem8 = new ArrayList<>();
        idItem8.add(new Id("2.16.156.10011.1.5","XXXXX"));
        providerOrganization.setId(idItem8);
        Name providName = new Name("LIST_EN");
        List<Value> LIST_EN_VALUE = new ArrayList<>();
        LIST_EN_VALUE.add(new Value("无锡中医院"));
        providName.setItem(LIST_EN_VALUE);
        providerOrganization.setName(providName);
        providerOrganization.setContactParty(new Device("CON"));
        patient.setProviderOrganization(providerOrganization);


        Receiver subjectOf1 = new Receiver("SBJ");
        Device queryMatchObservation = new Device("COND");
        queryMatchObservation.setMoodCode("EVN");
        queryMatchObservation.setCode(new Code("PDQ"));
        queryMatchObservation.setValue(new Name("INT","100"));
        subjectOf1.setQueryMatchObservation(queryMatchObservation);
        patient.setSubjectOf1(subjectOf1);

        Receiver coveredPartyOf = new Receiver("COV");
        Device coverageRecord = new Device("COV");
        coverageRecord.setMoodCode("EVN");
        Receiver RecBeneficiary = new Receiver("BEN");
        Device devBeneficiary = new Device("MBR");
        Code codeBeneficiary = new Code("1","2.16.156.10011.2.3.1.248");
        codeBeneficiary.setCodeSystemName("医疗保险类别代码");
        codeBeneficiary.setDisplayName(new Value("城镇职工基本医疗保险"));

        devBeneficiary.setCode(codeBeneficiary);
        RecBeneficiary.setBeneficiary(devBeneficiary);
        coverageRecord.setBeneficiary(RecBeneficiary);
        coveredPartyOf.setCoverageRecord(coverageRecord);
        patient.setCoveredPartyOf(coveredPartyOf);

        subject1.setPatient(patient);

        registrationEvent.setSubject1(subject1);
        //custodian
        Receiver custodian = new Receiver("CST");
        Device assignedEntity = new Device("ASSIGNED");
        List<Id> idItem9 = new ArrayList<>();
        idItem9.add(new Id("2.16.156.10011.0.3.2","登记人 ID"));
        assignedEntity.setId(idItem9);

        Device assignedPerson = new Device("PSN","INSTANCE");
        Name name = new Name();
        name.setXsiType("LIST_EN");
        List<Value> list2 = new ArrayList<>();
        list2.add(new Value("赵武"));
        name.setItem(list2);
        assignedPerson.setName(name);
        assignedEntity.setAssignedPerson(assignedPerson);
        custodian.setAssignedEntity(assignedEntity);
        registrationEvent.setCustodian(custodian);

        subject.setRegistrationEvent(registrationEvent);
        controlActProcess.setSubject(subject);
        Id queryAck = new Id();
        queryAck.setQueryId(new Id("2.16.156.10011.0","22a0f9e0-4454-11dc-a6be-3603d6866807"));
        queryAck.setQueryResponseCode(new Code("OK"));
        controlActProcess.setQueryAck(queryAck);
        prpa.setControlActProcess(controlActProcess);

        String s = XmlUtil.beanToXml(prpa, PRPA_IN201306UV02.class, "GBK");
        System.out.println(s);
    }
}
